/**
 * Created by lwy on 2017/8/30.
 */

var details;
var page = 1;
var id;

    $(function () {
    $.ajax({
        url: '/crm/ddmShopC/crmDetails.json',
        dataType: 'json',
        data: {
            token: localStorage.getItem('crmtoken'),
            id: $('#idBox').html(),
        },
        success: function (data) {
            if (data.code == -1) {
                window.parent.location.href = "/crm/login.jsp";
            }
            if (data.code == 0) {
                alert(data.msg);
            }
            if (data.code == 1) {
                var data = data.datas;
                $('#name').val(data.name);
                $('#location').val(data.location);
                $('#linkman').val(data.linkman);
                $('#contact').val(data.contact);
                $('#shopLevel').val(data.shopLevel);
                $('#state').val(data.state);
                $('#shopType').val(data.shopType);
                $('#longitudeAndLatitude').val(data.longitudeAndLatitude);
                $('#mainImg').attr('src', data.mainImg);
                $('#detailImg').append(data.datailImg);

                details = data.detailsImg;
                $('#detailImg img').load(function () {
                    $('#detailImg img').css({'width': '40px', 'height': '40px', 'margin-right': '10px'})
                })

                //点击放大图片
                $('#mainImg,#detailImg img').click(function () {
                    var src = $(this).attr('src');
                    $('#bigImg').attr('src', src);
                    $('#closeImg').show();
                })
                $('#closeImg,#close2').click(function () {
                    $('#closeImg').hide();
                })
                getUserList();

            }
        }
    })
})

function getUserList() {

    $.ajax({
        type: 'POST',
        url: '/crm/shopAdminUser/getShopUserList.json?',
        dataType: "json",   //参数格式为json
        data: {
            token: localStorage.getItem('crmtoken'),
            page:page,
            shopId: $('#idBox').html(),
            userStateValue:$('#userStateValue').val(),
        },
        success: function (data) {
            if (data.code == 0) {
                alert(data.msg);
            } else if (data.code == -1) {
                window.parent.location.href = "/crm/login.jsp";
            } else if (data.code == 1) {
                $('#record').html(data.recordsTotal);
                $('#shopUsertable').empty();
                $.each(data.datas, function (index, obj) {
                    var tdName = $("<tr><td>" + obj.shopAdminUserId + "</td>");
                    tdName.append($("<td>" + obj.userAccount + "</td>"))
                    tdName.append($("<td>" + obj.userRealName + "</td>"))
                    tdName.append($("<td>" + obj.userPhone + "</td>"));
                    tdName.append($("<td>" + obj.roleName + "</td>"));
                    $('#shopUsertable').append(tdName);
                });
                $(".pagination").createPage({
                    totalPage:data.totalPage,
                    currPage:data.pageNow,
                    pageCount:data.pageNow,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        getUserList();
                    }
                });
                getInitRoleUserSelect();
            }

        }
    });

}

//查询
$('#refer').click(function(){
    page=1;
    getUserList();
})

//获取查询后台成员下拉框
function getInitRoleUserSelect(){
    $.ajax({
        url:'/crm/userC/getInitRoleUserSelect.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
        },
        type:'post',
        success:function(data){
            if(data.code==1){
                $('#userName').removeData;
                for(var i=0;i<data.datas.length;i++){
                    $('<option value="'+data.datas[i].id+'">'+data.datas[i].name+'</option>').appendTo($('#userName'));
                }
            }
        }
    })
}




$('#memberUserAdd').click(function(){
    $.ajax({
        url:'/crm/shopAdminUser/addShopMember.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            userId:$('#userName').val(),
            //shopId:$('#shopName').val(),
            shopId: $('#idBox').html(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('添加成功');
                $('#memberUserModal').removeClass('fade in');
                $('#memberUserModal').addClass('fade');
                $('#memberUserModal').attr('aria-hidden',true);
                $('#memberUserModal').css('display','none');
                $('#memberUserModal input').val('');
                page=1;
                getUserList();
            }
        }
    })
})


$('#shopStart').click(function(){
    $.ajax({
        url:'/crm/ddmShopC/shopStart.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            id: $('#idBox').html(),

        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('启用店铺并铺货成功')
            }
        }
    })
})

$('#invalidShop').click(function(){
    $.ajax({
        url:'/crm/ddmShopC/invalidShop.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            id: $('#idBox').html(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('操作成功')
            }
        }
    })
})